package com.galen.manual.controller;

import com.galen.manual.common.Result;
import com.galen.manual.dto.AdminLoginDTO;
import com.galen.manual.dto.SendSmsDTO;
import com.galen.manual.dto.SmsLoginDTO;
import com.galen.manual.dto.UserLoginDTO;
import com.galen.manual.dto.UserProfileDTO;
import com.galen.manual.service.UserService;
import com.galen.manual.utils.UserContextUtil;
import com.galen.manual.vo.UserLoginVO;
import com.galen.manual.vo.UserInfoVO;
import com.galen.manual.vo.UserStatsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    @Operation(summary = "微信登录", description = "使用微信授权码进行登录")
    @PostMapping("/login")
    public Result<String> login(
            @Parameter(description = "登录信息", required = true) @Valid @RequestBody UserLoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        return Result.success("登录成功", token);
    }

    @Operation(summary = "发送短信验证码", description = "发送短信验证码到指定手机号")
    @PostMapping("/sms/send")
    public Result<String> sendSmsCode(
            @Parameter(description = "发送短信请求", required = true) @Valid @RequestBody SendSmsDTO sendSmsDTO,
            HttpServletRequest request) {
        // 获取客户端IP
        String clientIp = UserContextUtil.getClientIpAddress(request);
        userService.sendSmsCode(sendSmsDTO, clientIp);
        return Result.success("验证码发送成功");
    }

    @Operation(summary = "短信验证码登录", description = "使用手机号和验证码进行登录")
    @PostMapping("/sms/login")
    public Result<UserLoginVO> smsLogin(
            @Parameter(description = "短信登录请求", required = true) @Valid @RequestBody SmsLoginDTO loginDTO,
            HttpServletRequest request) {
        // 获取客户端IP
        String clientIp = UserContextUtil.getClientIpAddress(request);
        UserLoginVO loginVO = userService.smsLogin(loginDTO, clientIp);
        return Result.success("登录成功", loginVO);
    }

    @Operation(summary = "管理员登录", description = "使用管理员账号密码进行登录")
    @PostMapping("/admin/login")
    public Result<UserLoginVO> adminLogin(
            @Parameter(description = "管理员登录信息", required = true) @Valid @RequestBody AdminLoginDTO loginDTO,
            HttpServletRequest request) {
        // 获取客户端IP
        String clientIp = UserContextUtil.getClientIpAddress(request);
        UserLoginVO loginVO = userService.adminLogin(loginDTO, clientIp);
        return Result.success("管理员登录成功", loginVO);
    }

    @Operation(summary = "获取用户信息", description = "获取当前登录用户的详细信息")
    @GetMapping("/profile")
    public Result<UserInfoVO> getUserProfile() {
        UserInfoVO userInfo = userService.getUserProfile();
        return Result.success(userInfo);
    }

    @Operation(summary = "更新用户信息", description = "更新当前登录用户的信息")
    @PutMapping("/profile")
    public Result<Void> updateUserProfile(
            @Parameter(description = "用户信息", required = true) @Valid @RequestBody UserProfileDTO profileDTO) {
        userService.updateUserProfile(profileDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "获取用户统计", description = "获取当前登录用户的任务统计信息")
    @GetMapping("/stats")
    public Result<UserStatsVO> getUserStats() {
        UserStatsVO stats = userService.getUserStats();
        return Result.success(stats);
    }

}